<template>
  <BasicDrawer v-bind="$attrs" @register="registerDrawer" destroyOnClose showFooter title="复制广告" :width="drawerWith" @ok="handleSubmit">
    <BasicForm @register="registerForm"></BasicForm>
  </BasicDrawer>
</template>
<script lang="ts" setup>
import { defineEmits, ref, unref } from 'vue';
import { BasicForm, useForm } from '@apps/antd-admin/src/components/form';
import { BasicDrawer, useDrawerInner } from '@apps/antd-admin/src/components/drawer';
import { copyFormSchema } from '../data/data';
import { copyAdv } from '../data/api';
import { useAppInject } from '@apps/antd-admin/src/hooks/web/use-app-inject';
const { getIsMobile } = useAppInject();
const drawerWith = ref('35%');
if (unref(getIsMobile)) {
  drawerWith.value = '80%';
}
const emit = defineEmits(['register', 'success']);
const [registerForm, { resetFields, setFieldsValue, validate, setProps }] = useForm({
  labelWidth: 150,
  labelCol: { span: 6 },
  schemas: copyFormSchema,
  showActionButtonGroup: false,
  showAdvancedButton: false,
});

const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async data => {
  resetFields();
  setDrawerProps({ confirmLoading: false });
  await setFieldsValue({
    ...data.record,
  });
});

async function handleSubmit() {
  try {
    const values = await validate();
    setDrawerProps({ confirmLoading: true });
    await copyAdv(values);
    closeDrawer();
    emit('success', true);
  } finally {
    setDrawerProps({ confirmLoading: false });
  }
}
</script>
